package com.thermal.thermal_inspection.controller;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.Classes;
import com.thermal.thermal_inspection.entity.RealName;
import com.thermal.thermal_inspection.service.ClassService;
import com.thermal.thermal_inspection.vo.AddUserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users/class")
public class ClassController {
    @Autowired
    ClassService classService;

    private static final Logger logger = Logger.getLogger(ClassController.class);

    /**
     *新增班组
     */
    @PostMapping("/addClass")
    public CommonResponse<Object> addClass(@RequestParam String className){
        if(classService.existClassName(className)){
            logger.error("班组名已存在");
            return CommonResponse.createForError("班组名已存在");
        }
        int rows = classService.addClass(className);
        if(rows != 0) {
            logger.info("新增班级成功");
            return CommonResponse.createForSuccess("SUCCESS", null);
        }
        logger.error("新增班级失败");
        return CommonResponse.createForError("新增班级失败");
    }

    /**
     * 班组新增成员
     */
    @PostMapping("/addUser")
    public CommonResponse<Object> addUser(@RequestBody AddUserVO addUserVO){
        int classId = addUserVO.getClassId();
        List<Integer> userIdList = addUserVO.getUserIdList();
        for(int userId: userIdList){
            int rows = classService.addUser(classId, userId);
            switch (rows){
                case -1: {
                    logger.error("班组新增成员失败，班组不存在");
                    return CommonResponse.createForError("班组新增成员失败，班组不存在");
                }
                case -2: {
                    logger.error("班组新增成员失败，成员已在该班组中");
                    return CommonResponse.createForError("班组新增成员失败，成员已在该班组中");
                }
                case 0: {
                    logger.error("班组新增成员失败");
                    return CommonResponse.createForError("班组新增成员失败");
                }
            }
        }
        logger.info("班组新增成员成功");
        return CommonResponse.createForSuccess("SUCCESS", null);
    }

    /**
     * 查看班组成员
     */
    @GetMapping("/getClassMembers")
    public CommonResponse<List<RealName>> getClassMembers(@RequestParam int classId){
        if(!classService.existClass(classId)) {
            logger.error("该班组不存在");
            return CommonResponse.createForError("该班组不存在");
        }
        List<RealName> members = classService.getClassMembers(classId);
        logger.info("查看班组人员成功");
        return CommonResponse.createForSuccess("SUCCESS", members);
    }

    /**
     * 删除班组
     */
    @DeleteMapping("/deleteClass")
    public CommonResponse<Object> deleteClass(@RequestParam int classId){
        if(classId == -1) {
            logger.error("删除失败，该班组无法删除");
            return CommonResponse.createForError("删除失败，该班组无法删除");
        }
        if(classService.isClassInUse(classId)){
            logger.info("该班组正在使用中，无法删除");
            return CommonResponse.createForError("该班组正在使用中，无法删除");
        }

        int rows = classService.deleteClass(classId);
        if(rows != 0) {
            logger.info("删除成功");
            return CommonResponse.createForSuccess("SUCCESS", null);
        }
        logger.error("删除失败");
        return CommonResponse.createForError("删除失败");
    }

    /**
     * 删除班组成员
     */
    @DeleteMapping("/deleteUser")
    public CommonResponse<Object> deleteUser(@RequestParam int classId, @RequestParam int userId){
        //先看班组是否存在
        if(!classService.existClass(classId)) {
            logger.error("删除班组成员失败，班组不存在");
            return CommonResponse.createForError("删除班组成员失败，班组不存在");
        }
        int rows = classService.deleteUser(classId, userId);
        if(rows != 0) {
            logger.info("删除班组成员成功");
            return CommonResponse.createForSuccess("SUCCESS", null);
        }
        logger.error("删除班组成员失败");
        return CommonResponse.createForError("删除班组成员失败");
    }

    /**
     * 查找所有班组
     */
    @GetMapping("/getClasses")
    public CommonResponse<List<Classes>> getClasses(){
        List<Classes> classesList = classService.getClasses();
        if(!classesList.isEmpty()) {
            logger.info("查找所有班组成功");
            return CommonResponse.createForSuccess("SUCCESS", classesList);
        }
        logger.error("查找所有班组失败");
        return CommonResponse.createForError("查找所有班组失败");
    }

    /**
     * 查询特定班组
     */
    @GetMapping("/getClass/{classId}")
    public CommonResponse<Classes> getOneClass(@PathVariable int classId){
        if(!classService.existClass(classId)){
            logger.error("班组不存在，查询失败");
            return CommonResponse.createForError("班组不存在，查询失败");
        }
        Classes theClass = classService.getOneClass(classId);
        if(theClass == null){
            logger.error("查询特定班组失败");
            return CommonResponse.createForError("查询特定班组失败");
        }
        return CommonResponse.createForSuccess("SUCCESS", theClass);
    }
}
